﻿[assembly: System.Web.UI.TagPrefix("GSC.WebControls", "GSC")]
namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;
    using ComponentModel.Design;
    /// <summary>
    /// 后台中间头部控件。
    /// </summary>
    /// <remarks>后台中间头部控件。</remarks>
    [ToolboxItemFilter("GSC.WebControls", ToolboxItemFilterType.Custom)]
    [ToolboxData("<{0}:BackgroundCenterHeadControl runat=server height=30px width=100%></{0}:BackgroundCenterHeadControl>")]
    [SRDescription("Manage_CenterHead", typeof(Properties.Resources))]
    [Designer(typeof(BackgroundCenterHeadControlDesigner))]
    public class BackgroundCenterHeadControl : CompositeControl, INamingContainer, ICompositeControlDesignerAccessor
    {
        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;
            }
        }
        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "table";
            }
        }
        private TemplateTableCellOwner _leftCell;
        private TemplateTableCellOwner _centerCell;
        private TableCell _rightCell;
        ITemplate _leftCellTemplate;
        private HtmlGenericControl _tbody;
        TableRow _row;
        ITemplate _centerCellTemplate;
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>GuoShuCan.SDK.Web.TemplatePanelOwner</value>
        [Browsable(false)]
        public TemplateTableCellOwner LeftCell
        {
            get
            {
                return _leftCell;
            }
        }
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>TemplateTableCellOwner</value>
        [Browsable(false)]
        public TemplateTableCellOwner CenterCell
        {
            get
            {
                return _centerCell;
            }
        }
        /// <summary>
        /// 获取左边菜单顶部对象。
        /// </summary>
        /// <remarks>获取左边菜单顶部对象。</remarks>
        /// <value>TableCell</value>
        [Browsable(false)]
        public TableCell RightCell
        {
            get
            {
                return _rightCell;
            }
        }

        /// <summary>
        /// 获取或设置左边模板对象。
        /// </summary>
        /// <remarks>获取或设置左边模板对象。</remarks>
        /// <value>System.Web.UI.ITemplate</value>
        [
                      Browsable(false),
                      PersistenceMode(PersistenceMode.InnerProperty),
                      DefaultValue(typeof(ITemplate), null),
                      Description(""),
                      TemplateContainer(typeof(BackgroundCenterHeadControl))
                      ]
        public ITemplate LeftCellTemplate
        {
            get
            {
                return _leftCellTemplate;
            }
            set
            {
                _leftCellTemplate = value;
            }
        }

        /// <summary>
        /// 获取或设置左边背景图片。
        /// </summary>
        /// <remarks>获取或设置左边背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterHead_LeftCellBackImage", typeof(Properties.Resources))]
        public string LeftCellBackImage
        {
            get
            {
                object o = this.ViewState["LeftCellBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["LeftCellBackImage"] = value;
            }
        }

        /// <summary>
        /// </summary>
        /// <remarks></remarks>
        /// <value>System.Web.UI.HtmlControls.HtmlGenericControl</value>
        [Browsable(false)]
        public HtmlGenericControl Tbody
        {
            get
            {
                return _tbody;
            }
        }

        /// <summary>
        /// 获取行对象。
        /// </summary>
        /// <remarks>获取行对象。</remarks>
        /// <value>System.Web.UI.WebControls.TableRow</value>
        [Browsable(false)]
        public TableRow Row
        {
            get
            {
                return _row;
            }
        }

        /// <summary>
        /// 获取或设置中间背景图片。
        /// </summary>
        /// <remarks>获取或设置中间背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterHead_CenterCellBackImage", typeof(Properties.Resources))]
        public string CenterCellBackImage
        {
            get
            {
                object o = this.ViewState["BackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["BackImage"] = value;
            }
        }

        /// <summary>
        /// 获取或设置右边背景图片。
        /// </summary>
        /// <remarks>获取或设置右边背景图片。</remarks>
        /// <value>string</value>
        [Category("Appearance")]
        [DefaultValue("")]
        [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        [Bindable(true)]
        [Browsable(true)]
        [SRDescription("Manage_CenterHead_RightCellBackImage", typeof(Properties.Resources))]
        public string RightCellBackImage
        {
            get
            {
                object o = this.ViewState["RightCellBackImage"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["RightCellBackImage"] = value;
            }
        }
        /// <summary>
        /// 获取或设置左边模板对象。
        /// </summary>
        /// <remarks>获取或设置左边模板对象。</remarks>
        /// <value>System.Web.UI.ITemplate</value>
        [
                             Browsable(false),
                             PersistenceMode(PersistenceMode.InnerProperty),
                             DefaultValue(typeof(ITemplate), null),
                             Description(""),
                             TemplateContainer(typeof(BackgroundCenterHeadControl))
                             ]
        public ITemplate CenterCellTemplate
        {
            get
            {
                return _centerCellTemplate;
            }
            set
            {
                _centerCellTemplate = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (!this.DesignMode)
            {
                base.OnPreRender(e);
            }
            this.Attributes["cellPadding"] = "0";
            this.Attributes["cellSpacing"] = "0";
            this.Attributes["rules"] = "none";
            this.Style["table-layout"] = "fixed";
            this.Style["overflow"] = "hidden";
            //
            this.LeftCell.Height = this.Height;
            this.LeftCell.Width = this.LeftCellWidth;
            this.LeftCell.Style["overflow"] = "hidden";
            this.LeftCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.LeftCellBackImage))
                this.LeftCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Left.gif")) + ")";
            else
                this.LeftCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.LeftCellBackImage) + ")";
            //
            this.CenterCell.Height = this.Height;
            if (this.CenterCellWidth.IsEmpty)
                this.CenterCell.Style["width"] = "auto";
            else
                this.CenterCell.Width = this.CenterCellWidth;
            this.CenterCell.Style["overflow"] = "hidden";
            this.CenterCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.CenterCellBackImage))
                this.CenterCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Center.gif")) + ")";
            else
                this.CenterCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.CenterCellBackImage) + ")";
            //
            this.RightCell.Height = this.Height;
            this.RightCell.Width = this.RightCellWidth;
            this.RightCell.Style["overflow"] = "hidden";
            this.RightCell.Style["background-position"] = "left top";
            if (string.IsNullOrEmpty(this.RightCellBackImage))
                this.RightCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.GetWebResourceUrl("Right.gif")) + ")";
            else
                this.RightCell.Style["background-image"] = "url(" + this.ResolveClientUrl(this.RightCellBackImage) + ")";
        }
        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.DesignMode)
            {
                this.OnPreRender(EventArgs.Empty);
            }
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }
        UpdatePanel _leftCellUpdatePanel;
        UpdatePanel _centerCellUpdatePanel;
        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            this._tbody = new HtmlGenericControl("tbody");
            this._row = new TableRow();
            this._row.ID = "Row";
            this._tbody.Controls.Add(this._row);
            //
            this._leftCell = new TemplateTableCellOwner();
            this._leftCell.ID = "Left";
            //
            this._centerCell = new TemplateTableCellOwner();
            this._centerCell.ID = "Center";
            //
            this._rightCell = new TemplateTableCellOwner();
            this._rightCell.ID = "Right";
            //
            ITemplate temp = this._leftCellTemplate;
            if (temp == null)
            {
                if (this.DesignMode)
                {
                    temp = new DefaultEmptyTemplate();
                    temp.InstantiateIn(this._leftCell);
                }
                else
                {
                    if (this._leftCellUpdatePanel == null)
                    {
                        this._leftCellUpdatePanel = new UpdatePanel();
                    }
                    this._leftCellUpdatePanel.ID = "LeftUpdatePanel";
                    this._leftCellUpdatePanel.UpdateMode = this.LeftUpdateMode;
                    this._leftCellUpdatePanel.RenderMode = this.LeftRenderMode;
                    this._leftCell.Controls.Add(this._leftCellUpdatePanel);
                }
            }
            else
            {
                temp.InstantiateIn(this._leftCell);
            }
            //
            temp = this._centerCellTemplate;
            if (temp == null)
            {
                if (this.DesignMode)
                {
                    temp = new DefaultEmptyTemplate();
                    temp.InstantiateIn(this._centerCell);
                }
                else
                {
                    if (this._centerCellUpdatePanel == null)
                    {
                        this._centerCellUpdatePanel = new UpdatePanel();
                    }
                    this._centerCellUpdatePanel.ID = "CenterUpdatePanel";
                    this._centerCellUpdatePanel.UpdateMode = this.CenterUpdateMode;
                    this._centerCellUpdatePanel.RenderMode = this.CenterRenderMode;
                    this._centerCell.Controls.Add(this._centerCellUpdatePanel);
                }
            }
            else
            {
                temp.InstantiateIn(this._centerCell);
            }
            //
            this._row.Cells.Add(this._leftCell);
            this._row.Cells.Add(this._centerCell);
            this._row.Cells.Add(this._rightCell);
            //
            this.Controls.Add(this._tbody);
            //
            this._leftCell.DataBind();
            this._centerCell.DataBind();
            //this._rightCell.DataBind();
        }

        /// <summary>
        /// 设计时获取资源文件地址。
        /// </summary>
        /// <remarks>设计时获取资源文件地址。</remarks>
        /// <returns>string</returns>
        public virtual string GetWebResourceUrl(string resourceUrl)
        {
            if (this.Page == null) return string.Empty;
            if (this.Page.ClientScript == null) return string.Empty;
            return this.Page.ClientScript.GetWebResourceUrl(typeof(BackgroundCenterHeadControl), "GSC.WebControls.BackgroundCenterHeadControl.Images." + resourceUrl);
        }

        /// <summary>
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateControlHierarchy();
            ClearChildViewState();
        }

        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }

        /// <summary>
        /// 获取或设置里左边例宽度。
        /// </summary>
        /// <remarks>获取或设置里左边例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [DefaultValue(typeof(Unit), "800px")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_LeftCellWidth", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        public Unit LeftCellWidth
        {
            get
            {
                object o = this.ViewState["LeftCellWidth"];
                if (o == null) return Unit.Pixel(800);
                return (Unit)o;
            }
            set
            {
                this.ViewState["LeftCellWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置里中间例宽度。
        /// </summary>
        /// <remarks>获取或设置里中间例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [DefaultValue(typeof(Unit), "Empty")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_CenterCellWidth", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        public Unit CenterCellWidth
        {
            get
            {
                object o = this.ViewState["CenterCellWidth"];
                if (o == null) return Unit.Empty;
                return (Unit)o;
            }
            set
            {
                this.ViewState["CenterCellWidth"] = value;
            }
        }

        /// <summary>
        /// 获取或设置里右边例宽度。
        /// </summary>
        /// <remarks>获取或设置里右边例宽度。</remarks>
        /// <value>System.Web.UI.WebControls.Unit</value>
        [Browsable(true)]
        [DefaultValue(typeof(Unit), "28px")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_RightCellWidth", typeof(Properties.Resources))]
        [Category("Layout")]
        [TypeConverter(typeof(System.Web.UI.WebControls.UnitConverter))]
        public Unit RightCellWidth
        {
            get
            {
                object o = this.ViewState["RightCellWidth"];
                if (o == null) return Unit.Pixel(28);
                return (Unit)o;
            }
            set
            {
                this.ViewState["RightCellWidth"] = value;
            }
        }
        /// <summary>
        /// 获取或设置左边 UpdatePanel 控件的内容是否包含在 HTML &lt;div&gt; 或 &lt;span&gt; 元素中。
        /// </summary>
        /// <remarks>获取或设置左边 UpdatePanel 控件的内容是否包含在 HTML &lt;div&gt; 或 &lt;span&gt; 元素中。</remarks>
        /// <value>System.Web.UI.UpdatePanelRenderMode</value>
        [Browsable(true)]
        [DefaultValue(typeof(UpdatePanelRenderMode), "Block")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_LeftRenderMode", typeof(Properties.Resources))]
        [Category("Layout")]
        public UpdatePanelRenderMode LeftRenderMode
        {
            get
            {
                object o = this.ViewState["LeftRenderMode"];
                if (o == null) return UpdatePanelRenderMode.Block;
                return (UpdatePanelRenderMode)o;
            }
            set
            {
                this.ViewState["LeftRenderMode"] = value;
            }
        }
        /// <summary>
        /// 获取或设置中间 UpdatePanel 控件的内容是否包含在 HTML &lt;div&gt; 或 &lt;span&gt; 元素中。
        /// </summary>
        /// <remarks>获取或设置中间 UpdatePanel 控件的内容是否包含在 HTML &lt;div&gt; 或 &lt;span&gt; 元素中。</remarks>
        /// <value>System.Web.UI.UpdatePanelRenderMode</value>
        [Browsable(true)]
        [DefaultValue(typeof(UpdatePanelRenderMode), "Block")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_CenterRenderMode", typeof(Properties.Resources))]
        [Category("Layout")]
        public UpdatePanelRenderMode CenterRenderMode
        {
            get
            {
                object o = this.ViewState["CenterRenderMode"];
                if (o == null) return UpdatePanelRenderMode.Block;
                return (UpdatePanelRenderMode)o;
            }
            set
            {
                this.ViewState["CenterRenderMode"] = value;
            }
        }
        /// <summary>
        /// 获取或设置何时更新中间 UpdatePanel 控件的内容。
        /// </summary>
        /// <remarks>获取或设置何时更新中间 UpdatePanel 控件的内容。</remarks>
        /// <value>System.Web.UI.UpdatePanelUpdateMode</value>
        [Browsable(true)]
        [DefaultValue(typeof(UpdatePanelUpdateMode), "Always")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_CenterUpdateMode", typeof(Properties.Resources))]
        [Category("Layout")]
        public UpdatePanelUpdateMode CenterUpdateMode
        {
            get
            {
                object o = this.ViewState["CenterUpdateMode"];
                if (o == null) return UpdatePanelUpdateMode.Always;
                return (UpdatePanelUpdateMode)o;
            }
            set
            {
                this.ViewState["CenterUpdateMode"] = value;
            }
        }
        /// <summary>
        /// 获取或设置何时更新左边 UpdatePanel 控件的内容。
        /// </summary>
        /// <remarks>获取或设置何时更新左边 UpdatePanel 控件的内容。</remarks>
        /// <value>System.Web.UI.UpdatePanelUpdateMode</value>
        [Browsable(true)]
        [DefaultValue(typeof(UpdatePanelUpdateMode), "Always")]
        [Bindable(true)]
        [SRDescription("Manage_CenterHead_LeftUpdateMode", typeof(Properties.Resources))]
        [Category("Layout")]
        public UpdatePanelUpdateMode LeftUpdateMode
        {
            get
            {
                object o = this.ViewState["LeftUpdateMode"];
                if (o == null) return UpdatePanelUpdateMode.Always;
                return (UpdatePanelUpdateMode)o;
            }
            set
            {
                this.ViewState["LeftUpdateMode"] = value;
            }
        }
        /// <summary>
        /// 获取左边 UpdatePanel 控件。
        /// </summary>
        /// <remarks>获取左边 UpdatePanel 控件。</remarks>
        /// <value>System.Web.UI.UpdatePanel</value>
        [Browsable(false)]
        public UpdatePanel LeftUpdatePanel
        {
            get
            {
                if (_leftCellUpdatePanel == null && this._leftCellTemplate == null)
                {
                    _leftCellUpdatePanel = new UpdatePanel();
                }
                return _leftCellUpdatePanel;
            }
        }
        /// <summary>
        /// 获取中间 UpdatePanel 控件。
        /// </summary>
        /// <remarks>获取中间 UpdatePanel 控件。</remarks>
        /// <value>System.Web.UI.UpdatePanel</value>
        [Browsable(false)]
        public UpdatePanel CenterUpdatePanel
        {
            get
            {
                if (_centerCellUpdatePanel == null && this._centerCellTemplate == null)
                {
                    _centerCellUpdatePanel = new UpdatePanel();
                }
                return _centerCellUpdatePanel;
            }
        }
        /// <summary>
        /// 更新内部所有 UpdatePanel 控件。
        /// </summary>
        /// <remarks>更新内部所有 UpdatePanel 控件。</remarks>
        public void AllUpdate()
        {
            if (this._leftCellUpdatePanel != null)
            {
                this._leftCellUpdatePanel.Update();
            }
            if (this._centerCellUpdatePanel != null)
            {
                this._centerCellUpdatePanel.Update();
            }
        }
    }
}
